//
//  CellWithImages.m
//  shusheng
//
//  Created by rimi on 16/7/16.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "CellWithImages.h"
#import "SDAutoLayout.h"

@implementation CellWithImages

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = RGBCOLOR(229, 229, 229);
        [self initializeUserInterface];
    }
    return self;
}




- (void)initializeUserInterface{
    
   
    [self.contentView addSubview:self.backView];
//    [self.backView sendSubviewToBack:self.contentView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(5);
        make.left.equalTo(self).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.bottom.equalTo(self).with.offset(-5);
    }];
    
    
//    self.backView.sd_layout
//    .topSpaceToView(self.contentView,5)
//    .leftSpaceToView(self.contentView,0)
//    .rightSpaceToView(self.contentView,0)
//    .bottomSpaceToView(self.contentView,-5);
    

    
    [self.contentView addSubview:self.articleName];
//    [self.articleName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).with.offset(10);
//        make.left.equalTo(self).with.offset(10);
//        make.right.equalTo(self).with.offset(-10);
//        make.height.mas_equalTo(25);
//    }];
//
        self.articleName.sd_layout
        .topSpaceToView(self.contentView,10)
        .leftSpaceToView(self.contentView,10)
    .rightSpaceToView(self.contentView,10);
    self.articleName.sd_layout.autoHeightRatio(0);
    
    
    [self.contentView addSubview:self.detailName];
//    [self.detailName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.articleName.mas_bottom).with.offset(5);
//        make.left.equalTo(self).with.offset(10);
//        make.right.equalTo(self).with.offset(-10);
//        make.height.mas_equalTo(40);
//    }];
    self.detailName.sd_layout
    .topSpaceToView(self.articleName,5)
    .leftSpaceToView(self.contentView,10)
    .rightSpaceToView(self.contentView,10);
    self.detailName.sd_layout.autoHeightRatio(0);
    
    [self.contentView addSubview:self.imagesView1];
//    [self.imagesView1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.detailName.mas_bottom).with.offset(5);
//        make.left.equalTo(self).with.offset(10);
//        make.width.mas_equalTo(SCREEN_WIDTH/3 - 10);
//        make.height.mas_equalTo(SCREEN_WIDTH/3 - 10);
//    }];
    self.imagesView1.sd_layout
    .topSpaceToView(self.detailName,5)
    .leftSpaceToView(self.contentView,10)
    .widthIs(SCREEN_WIDTH/3 - 10)
    .heightIs(SCREEN_WIDTH/3 - 10);
    
    
    
    [self.contentView addSubview:self.imagesView2];
//    [self.imagesView2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.detailName.mas_bottom).with.offset(5);
//        make.left.mas_equalTo(self.imagesView1.mas_right).with.offset(5);
//        make.width.mas_equalTo(SCREEN_WIDTH/3 - 10);
//        make.height.mas_equalTo(SCREEN_WIDTH/3 - 10);
//    }];
    self.imagesView2.sd_layout
    .topEqualToView(self.imagesView1)
    .leftSpaceToView(self.imagesView1,5)
    .widthEqualToHeight(SCREEN_WIDTH/3 - 10)
    .heightIs(SCREEN_WIDTH/3 - 10);
    
    
    [self.contentView addSubview:self.imagesView3];
//    [self.imagesView3 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.detailName.mas_bottom).with.offset(5);
//        make.left.mas_equalTo(self.imagesView2.mas_right).with.offset(5);
//        make.width.mas_equalTo(SCREEN_WIDTH/3 - 10);
//        make.height.mas_equalTo(SCREEN_WIDTH/3 - 10);
//    }];
    self.imagesView3.sd_layout
    .topEqualToView(self.imagesView1)
    .leftSpaceToView(self.imagesView2,5)
    .widthEqualToHeight(SCREEN_WIDTH/3 - 10)
    .heightIs(SCREEN_WIDTH/3 - 10);

    [self.contentView addSubview:self.authorName];
//    [self.authorName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.imagesView1.mas_bottom).with.offset(10);
//        make.left.equalTo(self).with.offset(10);
//        make.right.equalTo(self.mas_centerX).with.offset(-10);
//        make.height.mas_equalTo(20);
//    }];
    self.authorName.sd_layout
    .topSpaceToView(self.imagesView1,10)
    .leftSpaceToView(self.contentView,10)
    .widthIs(SCREEN_WIDTH/2 - 10)
    .heightIs(20);
    
    [self.contentView addSubview:self.zanButton];
//    [self.zanButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.imagesView1.mas_bottom).with.offset(10);
//        make.right.equalTo(self).with.offset(-60);
//        make.width.mas_equalTo(30);
//        make.height.mas_equalTo(20);
//    }];
    self.zanButton.sd_layout
    .topSpaceToView(self.imagesView1,10)
    .rightSpaceToView(self.contentView,60)
    .widthIs(30)
    .heightIs(20);
    
    
    [self.contentView addSubview:self.talkButton];
//    [self.talkButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.imagesView1.mas_bottom).with.offset(10);
//        make.right.equalTo(self).with.offset(-30);
//        make.width.mas_equalTo(30);
//        make.height.mas_equalTo(20);
//    }];
    self.talkButton.sd_layout
    .topEqualToView(self.zanButton)
    .rightSpaceToView(self.contentView,30)
    .widthIs(30)
    .heightIs(20);
    
    
    [self setupAutoHeightWithBottomView:self.zanButton bottomMargin:10];
    
}


#pragma mark -- Getters
- (UILabel *)articleName{
    if (!_articleName) {
        _articleName = [[UILabel alloc] init];
        _articleName.font = [UIFont boldSystemFontOfSize:18];
//        _articleName.backgroundColor = [UIColor orangeColor];
        
    }
    return _articleName;
}

- (UILabel *)detailName{
    if (!_detailName) {
        _detailName = [[UILabel alloc] init];
        _detailName.font = [UIFont systemFontOfSize:15];
        [_detailName setTextColor:[UIColor darkGrayColor]];
//        _detailName.backgroundColor = [UIColor orangeColor];
        _detailName.numberOfLines = 2;
        
    }
    return _detailName;
}

- (UILabel *)authorName{
    if (!_authorName) {
        _authorName = [[UILabel alloc] init];
        _authorName = [[UILabel alloc] init];
        _authorName.font = [UIFont systemFontOfSize:15];
        [_authorName setTextColor:[UIColor lightGrayColor]];
//        _authorName.backgroundColor = [UIColor orangeColor];
        
    }
    return _authorName;
}

- (UIImageView *)imagesView1{
    if (!_imagesView1) {
        _imagesView1 = [[UIImageView alloc] init];
//        _imagesView1.backgroundColor = [UIColor redColor];
    }
    return _imagesView1;
}


- (UIImageView *)imagesView2{
    if (!_imagesView2) {
        _imagesView2 = [[UIImageView alloc] init];
//        _imagesView2.backgroundColor = [UIColor redColor];
    }
    return _imagesView2;
}


- (UIImageView *)imagesView3{
    if (!_imagesView3) {
        _imagesView3 = [[UIImageView alloc] init];
//        _imagesView3.backgroundColor = [UIColor redColor];
    }
    return _imagesView3;
}

- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}

- (UIButton *)zanButton{
    if (!_zanButton) {
        _zanButton = [[UIButton alloc] init];
        [_zanButton setImage:[UIImage imageNamed:@"zan2.png"] forState:UIControlStateNormal];
        _zanButton.titleLabel.text = @"(0)";
        [_zanButton setImage:[UIImage imageNamed:@"selectedZan.png"] forState:UIControlStateSelected];
        
        _zanButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        [_zanButton addTarget:self action:@selector(respondsToZanButton:) forControlEvents:UIControlEventTouchUpInside];
//        _zanButton.backgroundColor = [UIColor blueColor];
        
    }
    return _zanButton;
}


- (UIButton *)talkButton{
    if (!_talkButton) {
        _talkButton = [[UIButton alloc] init];
        [_talkButton setImage:[UIImage imageNamed:@"评论.png"] forState:UIControlStateNormal];
        _talkButton.titleLabel.text = @"(0)";
        _talkButton.titleLabel.textAlignment = NSTextAlignmentLeft;
//        _talkButton.backgroundColor = [UIColor blueColor];
    }
    return _talkButton;
}


- (void)respondsToZanButton:(UIButton *)sender{
    NSLog(@"谢谢你的赞");
    _zanButton.selected = !_zanButton.selected;
}

@end
